<?php
/**
 * This is a url helper that requires that you specify a route first. My applications tend
 * to always have route names for every single route. So, requiring a route name first
 * usually makes more sense in my apps.
 * 
 * Another feature of this app is the ability to specify either an array or a route name as
 * the first parameter. If the first param is  a string, it is assumed that it's a route name,
 * If it's an array, it assumes it's an array of params.
 */
class ImpSoft_View_Helper_RouteUrl extends Zend_View_Helper_Url {

    public function routeUrl($name = null, $urlOptions = array(), $getParams = array(), $reset = false, $encode = true) {
    
        $front = Zend_Controller_Front::getInstance();
        $routes = array_keys($front->getRouter()->getRoutes());
		if (is_null($urlOptions)) $urlOptions = array();
    	// if $name isn't an existing route, assume they meant to call a url
        if (!in_array($name, $routes)) {
            $url = rtrim($front->getBaseUrl(), '/') . '/';
            $url .= ltrim($name, "/");
        } else {
            if (is_null($name)) $name = "default";
            $url = parent::url($urlOptions, $name, $reset, $encode);
        }
		// put together the GET parameters
		$queryString = "";
		$queryPairs = array();
		foreach ($getParams as $name => $value) {
			$queryPairs[] = "$name=$value";
		}
		if (!empty($queryPairs)) {
			$queryString = "?" . implode("&", $queryPairs);
		}
        return $url . $queryString;
    
    }

}